<?php
namespace Admin\Controller;
use Think\Controller;
class BaseController extends Controller {   
    private $model;
    private $tbl;
    private $perpage;
    public function _initialize(){
    	$this->perpage=6;
      // session('admin',1);
      if(!session('?admin')){
        $this->redirect('admin.php/login/form');
        exit;}
    }
    public function showlist($tbl,$morder='zhr_id desc',$related=0){
      $this->model=D($tbl);	
      //$data=$this->model->order($morder)->select();
	  // $this->assign('list',$data);
      $count= $this->model->count();// 查询满足要求的总记录数
      $Page = new \Think\Page($count,$this->perpage);// 实例化分页类 传入总记录数和每页显示的记录数(25)
      $show = $Page->show();// 分页显示输出
// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
      if($related!==1)
        $list = $this->model->order($morder)->limit($Page->firstRow.','.$Page->listRows)->select();
      else
        $list = $this->model->relation(true)->order($morder)->limit($Page->firstRow.','.$Page->listRows)->select();
      $this->assign('list',$list);// 赋值数据集
      $this->assign('page',$show);// 赋值分页输出
      $this->display(); // 输出模板
    }

    public function view($tbl){
       $this->model=D($tbl);
       $id=I('get.id');
       $one=$this->model->find($id);//model->relation(true)->
       $this->assign('one',$one);
       $this->display();
    }
    
     public function form($tbl){  
       $this->model=D($tbl);
       $this->display();
    }

     public function index($tbl){  
       $this->model=D($tbl);
       $this->display();
    }
    
    public function add($tbl,$address='admin.php/user/userlist',$myfield='thumb'){
       $this->model=D($tbl);
       $data=I('post.');
       $data[$myfield]='';
       if(!empty($_FILES[$myfield])){
         $data[$myfield]=uploadfile($myfield,150);
      }
       if ($this->model->create($data)) {
       	$this->model->add();
       	$this->success('创建成功',U($address)); }
       else  $this->error($this->model->getError());
    }
    public function mod($tbl){
       $this->model=D($tbl);
       $id=I('get.id');
       $one=$this->model->find($id);//model->relation(true)->
       $this->assign('one',$one);
       $this->display();
    }

    public function update($tbl,$address='admin.php/user/userlist',$myfield='thumb'){
       $this->model=D($tbl);
       $data=I('post.');
       $data[$myfield]='';
       if(!empty($_FILES[$myfield]['name'])){
         $data[$myfield]=uploadfile($myfield,150);
      }else{
        unset($data[$myfield]);
      }
       if ($this->model->create($data)) {
        $this->model->save();
        $this->success('修改成功',U($address)); }
       else  $this->error($this->model->getError());
    }

     public function delete($tbl,$address='admin.php/user/userlist'){
       $this->model=D($tbl);
       $id=I('get.id',0);
       $result=$this->model->delete($id);
       if($result){
       	$this->success('删除成功',U($address));
       }
        else  $this->error($this->model->getError());
    }
    
    public function search($tbl,$search='search_key'){
       $this->model=D($tbl);
       $key=I('post.'.$search);
       $sql="select * from user where zhr_username like '%".$key."%'";
       $data=M()->query($sql);
       // M()->execute($sql);
       $this->assign('users',$data);
       $this->display();
    }
}
